/*
 * COAPI - Copyright (C) 2019-2020 Cocheer Tech. Co., Ltd.
 * All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <aos/kernel.h>
#include "coapi_logger.h"
#include "coapi_stream.h"
#include "coapi_record.h"
#include "coapi_cmd.h"
#include "coapi_comm.h"

#define CONF_PRINT_TIME

static unsigned char dlg_type;

#ifdef CONF_PRINT_TIME
struct timeval start;
struct timeval end;
#endif

#ifdef CONF_PRINT_TIME
static void print_delay_time(void)
{
    gettimeofday(&end, NULL);
	
    long delay_ms = (end.tv_sec - start.tv_sec) * 1000;
    long delay_us = 0;
    
	if ((delay_us = (end.tv_usec - start.tv_usec)) < 0) {
		delay_ms -= 1;
		delay_us += 1000;
    }
    long delay = delay_ms + delay_us / 1000;
}
#endif

static void __deel_task(void* arg)
{
#ifdef CONF_PRINT_TIME
    print_delay_time();
#endif
    if (dlg_type == STREAM_RSQ_FINAL) {
		stream_t* nor = (stream_t*)arg;
		coapi_player_start(nor->toneurl, nor->audiourl);
		/* 处理语音控制指令 */
		coapi_cmd_ctrl_cb(nor->action, nor->data);
		coapi_stream_free(nor);
    }
    aos_task_exit(0);
}

static void create_dlg_task(void* arg, int type)
{
    int ret = 0;

    dlg_type = (unsigned char)type;
    aos_task_t task_handle;
    ret = aos_task_new_ext(&task_handle, "__deel_task", __deel_task, arg, 1024*4, 10);
    if (ret != 0) {
		xerror("create ai feed thead failed");
    }
}

/* 返回结果处理 */
void voice_callback(void* arg, int type)
{
    /* 返回错误 */
    if (type == STREAM_RSQ_ERR) {
		xerror("ai %s ", (char*)arg);
		return;
    }

    /* 过程结果，可忽略不处理 */
    if (type == STREAM_RSQ_MID) {
		stream_req* req = (stream_req*)arg;
		xdebug("offset: %d, cid: %d, sid: %d , type: %d, data: %s, len: %d",
				req->offset, req->cid, req->sid, req->type, req->data,
				req->data_len);
		return;
    }

    /* 最终结果 */
    if (type == STREAM_RSQ_FINAL) {
		xdebug("ai rsq normal.");
		xdebug("url: %s", ((stream_t*)arg)->toneurl);

		stream_t* normaldlg = malloc(sizeof(stream_t));
		memset(normaldlg, 0, sizeof(stream_t));

		coapi_stream_cpy(normaldlg, (stream_t*)arg);
		create_dlg_task((void*)normaldlg, type);

		if (normaldlg->nlu) {
			xinfo("uln: %s", normaldlg->nlu);
		}

		if(normaldlg->in) {
			printf("\n in: %s\n",((stream_t*)arg)->in);
		}

		if(normaldlg->out) {
			printf("\n out: %s\n",((stream_t*)arg)->out);
		}
    }
    return;
}
/* 初始化和录音 */
void coapi_speech_start(void)
{
	int ret;
    /*
	* 初始化语音上传功能功能，一次上传调用一次
	* 语音上传结果回调函数   											voice_callback
	* 语音上传的功能为AI对话											FUNC_SPEECH
	* 结果返回对话内容													RESULT_SDS
	* 不使用CoAPI的 vad													AI_NO_VAD
	* 传入coapi_stream_feed接口的数据格式为 16bit 16kHz 的PCM数据 		TYPE_PCM
	*/
	ret = coapi_stream_init(voice_callback, FUNC_SPEECH, RESULT_SDS, AI_NO_VAD, TYPE_PCM);
    if (ret < 0) {
        xerror("not cloud registered !");
        return;
    }
    return;
}

/* 结束录音 */
void coapi_speech_stop(void)
{
#ifdef CONF_PRINT_TIME
    gettimeofday(&start, NULL);
#endif

  return;
}
